var searchTabs;
var selectedItem = null;

function showError(txt)
{
	alert(txt);
}
function showInfo(txt)
{
	alert(txt);
}
function applyFilters()
{
	var showFirstAvailable = false;
	
	for(var i = 0; i < searchFilter.length; i++)
	{
		if(searchFilter[i] == 0)
		{
			if(i == currentResultPanel)
			{
				showFirstAvailable = true;
			}
			
			$('result_panel_' + searchTypes[i]).style.display = 'none';
			$('tab_' + searchTypes[i]).style.display = 'none';
		}
		else
		{
			$('tab_' + searchTypes[i]).style.display = '';
			
			if(pendingSearches[i])
			{
				gotoPage(0, searchTypes[i]);
			}
		}
	}
	if(showFirstAvailable)
	{
		for(var i = 0; i < searchFilter.length; i++)
		{
			if(searchFilter[i] == 1)
			{
				searchTabs.pickTab(i);
				
				break;
			}
		}
	}
}

function createSettings()
{
	var filterContainer = $('filter_container');
	
	for(var i = 0; i < searchTypes.length; i++)
	{
		var typeElement = document.createElement('a');
		
		typeElement.href = "#";
		typeElement.id = 'media_filter_anchor_' + i;
			
		var check = document.createElement('img');
		
		check.id = 'media_check_' + i;
		
		check.checked = (searchFilter[i] == 1);

		check.src = check.checked ? 'images/checkbox_on.png' : 'images/checkbox_off.png';;
		
		typeElement.appendChild(check);
		
		typeElement.className = 'media_filter';

		typeElement.mediaIndex = i;
		
		typeElement.onclick = mediaFilterChecked;
		
		typeElement.appendChild(document.createTextNode(' ' + searchTypes[i]));
		
		filterContainer.appendChild(typeElement);
	}
}
function mediaFilterChecked()
{
	var check = $('media_check_' + this.mediaIndex);
	var filterName = $('media_filter_anchor_' + this.mediaIndex).text;
	
	check.checked = !check.checked;
	
	check.src = check.checked ? 'images/checkbox_on.png' : 'images/checkbox_off.png';
	
	// Since Translations are not compatible with others, if I check translations uncheck all others
	if (filterName == ' Translation') {
			alert('Disabling all other search types as they are not compatible with translations.');
			for (var i = 0; i < searchTypes.length; i++) {
			if (i == this.mediaIndex) {
				continue;
			}
			otherCheck = $('media_check_' + i);
			otherCheck.checked = false;
			otherCheck.src = 'images/checkbox_off.png';
		}
	} else {
		// Make sure Translation is off
		for (var i = 0; i < searchTypes.length; i++) {
			var otherName = $('media_filter_anchor_' + i).text;
			if (otherName == ' Translation') {
				otherCheck = $('media_check_' + i);
				if (otherCheck.checked) {
					alert('Disabling Translations as they are not compatible with other search types.');
				}
				otherCheck.checked = false;
				otherCheck.src = 'images/checkbox_off.png';
			}
		}
	}
}

function createUI()
{
	var tabs = new Tabs(searchTypes, tabHandler);
	
	searchTabs = tabs;
	
	$('search_tabs').innerHTML = '';
	
	tabs.appendTo($('search_tabs'));
	
	/* create panels */
	
	var panelsContainer = $('search_panels');
	
	for(var i = 0; i < searchTypes.length; i++)
	{
		var panel = createResultPanel(searchTypes[i]);
		
		panel.id = 'result_panel_' + searchTypes[i];
		
		if(i != 0)
			panel.style.display = 'none';
			
		panelsContainer.appendChild(panel);
	}
}

function showResults(res, types, defaultPanel)
{
	for(var i = 0; i < searchTypes.length; i++)
	{
		var typeName = searchTypes[i];
		
		var panel = $('result_container_' + searchTypes[i]);
			
		var typeIsSearched = false;
		
		INNER:for(var j = 0; j < types.length; j++)
		{
			if(types[j] == typeName)
			{
				typeIsSearched = true;
				
				break INNER;
			}
		}
		
		if(searchFilter[i] == 0)
		{
			panel.innerHTML = '';
			
			pendingSearches[i] = true;
		}
		else if(typeIsSearched)
		{
			panel.innerHTML = '';
			
			pendingSearches[i] = false;
			
			$('result_loader_' + typeName).style.display = 'none';
		
			if(typeName == 'Translation') {
				var topPageContainer = document.createElement('div');
				topPageContainer.className = 'result_pages';
				panel.appendChild(topPageContainer);

				var results = res.SearchResponse[typeName].Results;
	
				for(var j = 0; j < results.length; j++)
				{
					panel.appendChild(createResultItem(results[j], typeName));
				}
				
				panel.appendChild(pageContainer);				
			} else if(res.SearchResponse[typeName])
			{
				/* pagination */
				
				var total = res.SearchResponse[typeName].Total;
				
				var offset = res.SearchResponse[typeName].Offset;
				
				var currentPage = Math.ceil(offset / searchPerPage);
				
				var topPageContainer = document.createElement('div');
				topPageContainer.className = 'result_pages';
				panel.appendChild(topPageContainer);
				
				topPageContainer.appendChild(document.createTextNode(
					'Results ' + (offset + 1) + "-"  + Math.min(total, offset + searchPerPage) + ' of ' + total
				));
	
				var pageContainer = document.createElement('div');
				pageContainer.className = 'result_pages';
				
				pageContainer.appendChild(document.createTextNode(
					'Results ' + (offset + 1) + "-"  + Math.min(total, offset + searchPerPage) + ' of ' + total
				));
				
				var self = this;
				
				if(currentPage > 0)
				{
					var prevPage = currentPage - 1;
					var type = types[i];
					
					var prevPageEl = document.createElement('a');
					prevPageEl.href = "#";
					prevPageEl.searchType = typeName;
					prevPageEl.searchPage = prevPage;
					prevPageEl.className = 'result_page';
					prevPageEl.innerHTML = 'Previous';
					prevPageEl.onclick = pageLinkClick;
					pageContainer.appendChild(prevPageEl);
				}
				if(offset + searchPerPage < total)
				{
					var nextPage = currentPage + 1;
					var type = types[i];
					
					var nextPageEl = document.createElement('a');
					nextPageEl.href = "#";
					nextPageEl.searchType = typeName;
					nextPageEl.searchPage = nextPage;
					nextPageEl.className = 'result_page';
					nextPageEl.innerHTML = 'Next';
					nextPageEl.onclick = pageLinkClick;
					pageContainer.appendChild(nextPageEl);
				}
				
				var results = res.SearchResponse[typeName].Results;
	
				for(var j = 0; j < results.length; j++)
				{
					panel.appendChild(createResultItem(results[j], typeName));
				}
				
				panel.appendChild(pageContainer);
				
	//			panel.appendChild(pageContainer.cloneNode(true));
			}
			else
			{
				panel.innerHTML = 'No results';
			}
		}
		
	}
}
function pageLinkClick()
{
	document.body.scrollTop = 0;
	
	$('screen_search').scrollTop = 0;
	
	gotoPage(this.searchPage, this.searchType);
}
function createResultItem(item, type)
{
	var el = document.createElement('a');
	
	el.href = "#";
	
	el.className = 'result_item';

	var targetURL = item.Url;

	if(type == 'Image')
	{
		content = 
			'<div class="item_title">' + item.Title + '</div>' + 
			'<img class="item_thumb" src="' + item.Thumbnail.Url + '" />';
	}
	else if(type == 'News')
	{
		content = 
			'<div class="item_title">' + item.Title + '</div>' + 
			'<div class="item_description">' + item.Snippet + '</div>';
	}
	else if(type == 'Web')
	{
		content = 
			'<div class="item_title">' + item.Title + '</div>' + 
			'<div class="item_description">' + item.Description + '</div>';
	}
	else if(type == 'MobileWeb')
	{
		content = 
			'<div class="item_title">' + item.Title + '</div>' + 
			'<div class="item_description">' + item.Description + '</div>';
	}
	else if(type == 'Video')
	{
		targetURL = item.PlayUrl;
		
		content = 
			'<div class="item_title">' + item.Title + '</div>' + 
			'<img class="item_thumb" src="' + item.StaticThumbnail.Url + '" />';
	}
	else if(type == 'Translation')
	{
		content = 
			'<div class="item_title">' + item.TranslatedTerm + '</div>';
	}
	
	el.targetURL = targetURL;
	
	if(!is5thEdition())
	{
		el.innerHTML = content + 
			'<div class="item_link content_link">Open in Web Browser</div>';
		
		el.onclick = resultItemClick;
		
		el.onfocus = selectResultItem;
	}
	else
	{
		el.innerHTML = content + 
			'<a href="#" class="item_link content_link" ' + 
			'onclick="widget.openURL(\'' + targetURL + '\')">Open in Web Browser</a>';
		
		el.onclick = selectResultItem;
	}
		
	
	return el;
}
function selectResultItem(event)
{
	if(selectedItem != null)
	{
		selectedItem.className = 'result_item';
	}

	selectedItem = this;
	
	selectedItem.className = 'result_item_selected';
}
function resultItemClick()
{
	widget.openURL(this.targetURL);
}
function createResultPanel(typeName)
{
	var el = document.createElement('div');
	
	el.className = 'result_panel';
	
	
	
	var resultsContainer = document.createElement('div');
	
	resultsContainer.id = 'result_container_' + typeName;
	
	resultsContainer.innerHTML = 'Welcome the to the Bing widget! Type what you want to search, and then press the "Search" button. You\'ll see results for each category in each of the tabs above.';
	
	
	
	
	var elLoader = document.createElement('div');
	
	elLoader.className = 'result_loader';
	
	elLoader.id = 'result_loader_' + typeName;
	
	elLoader.innerHTML = 'Loading...';
	
	elLoader.style.display = 'none';
	
	el.appendChild(elLoader);
	el.appendChild(resultsContainer);
	
	return el;
}
